package com.google.code.server4j.connector.nio;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

import com.google.code.server4j.connector.nio.NioEndpoint.SelectorManager;

public class NioSocket {

	private SocketChannel channel;
	
	private ByteBuffer readBuffer;
	
	private ByteBuffer writeBuffer;
	
	private SelectorManager selectorManager;
	
	private volatile SocketConnectState state;
	
	private volatile long lastVisitTime;
	
	public int read(ByteBuffer buf) throws IOException {
		return channel.read(buf);
	}
	
	public int write(ByteBuffer buf) throws IOException {
		return channel.write(buf);
	}
	
	public void close() throws IOException {
		channel.close();
	}
	
	public boolean isOpen() {
		return channel.isOpen();
	}
	
	public void createBuffer(int readSize,int writeSize,boolean isDirect) {
		if(isDirect) {
			readBuffer = ByteBuffer.allocateDirect(readSize);
			writeBuffer = ByteBuffer.allocateDirect(writeSize);
		} else {
			readBuffer = ByteBuffer.allocate(readSize);
			writeBuffer = ByteBuffer.allocate(writeSize);
		}
	}
	
	public void associateChannel(SocketChannel channel) {
		this.channel = channel;
	}
	
	public void associateSelector(SelectorManager selectorManager) {
		this.selectorManager = selectorManager;
	}
	
	public ByteBuffer getReadBuffer() {
		return this.readBuffer;
	}
	
	public ByteBuffer getWriteBuffer() {
		return this.writeBuffer;
	}
	
	public SocketChannel getChannel() {
		return this.channel;
	}

	public SelectorManager getSelectorManager() {
		return selectorManager;
	}

	public SocketConnectState getState() {
		return state;
	}

	public void setState(SocketConnectState state) {
		this.state = state;
	}

	public long getLastVisitTime() {
		return lastVisitTime;
	}

	public void setLastVisitTime(long lastVisitTime) {
		this.lastVisitTime = lastVisitTime;
	}
	
	
}
